﻿#region usings

using System.Collections.Generic;
using System.Data;
using System.Linq;
using SimpleDataAccessConsole.DataAccessLayer;

#endregion

namespace SimpleDataAccessConsole
{
    internal class IsUserValid : SimpleQuery
    {
        public IsUserValid()
        {
            CommandText =
                "SELECT user_id, encrypted_password FROM usertable WHERE user_id = ? and encrypted_password = ?";
            CommandType = CommandType.Text;
        }
    }

    internal class Report001 : SimpleQuery
    {
        public Report001()
        {
            CommandText = "report001";
            CommandType = CommandType.StoredProcedure;
        }
    }

    internal class AppDatabase : Database
    {
        public AppDatabase(string providerName, string connectionString)
            : base(providerName, connectionString)
        {
        }

        public bool IsUserValid(string userName, string encryptedPassword)
        {
            var result = Execute(
                new IsUserValid
                    {
                        Parameters = new object[] {userName, encryptedPassword}
                    }
                ).ToList();


            return result.Count > 0;
        }

        public List<string> ExecuteReport(SimpleQuery query)
        {
            var result = base.Execute(query);

            return
                result.Select(row => row.Select(i => i.ToString()).ToArray()).Select(
                    fields => string.Format("\"{0}\"\n", string.Join("\",\"", fields))).ToList();
        }
    }
}